<?php
/**
 * 属性数据表
 *
 * @author YeFng
 */

namespace c\attribute;

class m extends \m\ad
{
    var $t_p_p = 'attribute/post';
    
    // 仅显示输入框，不读取数据
	function index_sw_post()
	{
        $this->PU->login();
        
		$pid = intval($_GET['type_id']);
        if (!$pid)
            die;
		$res = $this->db->get('attribute_type', '*', 'parent_id='.$pid.' AND state>0', 'ordering, id');
		$this->loadTemplate($this->t_p_p, array('res'=>$res) );
		$this->display('');
	}
    
    /**
     * 
     * @param type $type_id
     * @param type $data_id
     */
    function post($type_id, $data_id=0)
    {
        $data_id = intval($data_id);
		
		$tables = array('at'=>array('as'=>'attribute_type'));
		$tables['t1'] = array('as'=>$this->table, 'join'=>'LEFT JOIN', 'on'=>"t1.type_id=at.id AND t1.data_id=$data_id");
        $res = $this->db->get($tables, 'at.*, t1.value',"at.parent_id=$type_id AND at.state>0", 'at.ordering, at.id');
		$this->loadTemplate($this->t_p_p, array('res'=>$res) );
	}
    
    
	function save($data_id, $dataA=NULL)
	{
        if (!$dataA)
            $dataA = $_POST['attr'];
        
		$d = array('data_id'=>$data_id);
		foreach ($dataA as $k=>$v)
		 {            
            $d['type_id'] = $k;
            foreach ($v as $val)
             {
                if ($val==='')
                    continue;
                
                $d['value'] = $val;
                if ( !$this->db->save($this->table, $d))
                    return false;
             }	
		 }
		return true;	
	}
    function del($data_id)
    {
        return $this->db->del($this->table, "data_id IN($data_id)");
    }
    function editData($data_id)
    {
        $this->db->sqlS();
        $this->del($data_id);
        $b = $this->save($data_id);
        if ($b)
            $this->db->sqlE();
        else
            $this->db->sqlB();

        return $b;
    }
}
